// 2.Observer数据劫持：监听data属性的变化，有变化时拿到新值并通知Dep
class Observer {
    constructor(data) {
        this.walk(data)
    }
    walk(data) {
        if(!data || typeof data !== 'object') {
            return
        }
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }
    defineReactive(data, key, val) {
        const that = this
        let dep = new Dep()
        this.walk(val)

        Object.defineProperty(data, key, {
            configurable: true,
            enumerable: true,
            get() {
                dep.addSub(Dep.target)
                return val
            },
            set(newVal) {
                debugger
                if(newVal === val) {
                    return
                }
                val = newVal
                that.walk(newVal)

                // 触发通知
                dep.notify()
            }
        })
    }
}